<?php
namespace Kxdr\Controller;
use Think\Controller;
class IndexController extends ExtendController {
		public function show_tools(){
		$repick = I("get.repick");

		$Tool = D("Shopadmin/CheckTools");
		$Tool->sid=$this->sid;
		$Tool->where="`sm_tools`.`tool_type`=1 OR `sm_tools`.`tool_type`=3 OR `sm_tools`.`tool_type`=4";
		$tools = $Tool->ShowTools();
		$kxdr = $tools["kxdr"]; //我的互动单独列出
		unset($tools["kxdr"]);
		$this->assign('kxdr',$kxdr);
		$this->assign('tools',$tools);
		$this->assign('title','游戏设置');
		$this->assign("current","shopgamecfg");
		$this->display();
		}
		public function index(){
			$q = I("q");
			$t = I("t");
			$qw = $q?(" AND `title` LIKE '%".$q."%'"):"";
			$tw = $t?(($t==1)?(" AND `game`.`start_time` > NOW()"):(" AND `game`.`start_time` < NOW()")):"";
			$where = $qw.$tw." AND `event`.`version`='".session("app_version")."'";
			$where .=" AND `event`.`status`=1";
			Vendor("Page");
			$EVENT = D("EventView");
			$total = $EVENT->where("`event`.`sid`=".$this->sid.$where)->getField("count(`event`.`id`)");
			$PAGE = new \Page($total,10);
			$page = $PAGE->show();
			
			$CFG = M("ShopCfg");
			$curtool = $CFG->where("`sid`=".$this->sid)->getField("curtool");

			
			$events = $EVENT->where("`event`.`sid`=".$this->sid.$where)->order("`event`.`id` DESC")->limit($PAGE->firstRow.','.$PAGE->listRows)->getField("id,think_type,think_type_way,time_out,time_out_type,title,description,pic_url,logo_url,need_seeds,gifting_time,game_name,start_time,location,game_descrip,game_pic_url,player_name,player_descrip,event_player_descrip,is_team,game_id,player_id");
			$GFT = D("GiftView");
			// $GFT = D("KxdrEventGifts");
			$PLR = D("EventPlayerView");
			foreach($events as $k=>$e){
				if($e["time_out_type"]>0){
					$events[$k]["stop_time"]=strtotime($e["start_time"])-$e["time_out"]*60;
					$events[$k]["stop_time_str"]=date("m月d日H时i分",$events[$k]["stop_time"]);
				}else{
					$events[$k]["stop_time"]=strtotime($e["start_time"])+$e["time_out"]*60;
					$events[$k]["stop_time_str"]=date("m月d日H时i分",$events[$k]["stop_time"]);
				}
				if($events[$k]["stop_time"]<time()){
					$events[$k]["status"]="stopped";
				}else{
					$events[$k]["status"]="running";
				}
				// foreach(C("KXDR_THINK_WAY") as $way){
					// if($e["think_type_way"]==$way["id"]){
						// $events[$k]["think_type_way_name"]=$way["name"];break;
					// }
				// }
				$GFT->eid=$k;
				$GFT->sid=$this->sid;
				$GFT->getEventGifts();
				$events[$k]["gifts"]=$GFT->gifts;
				$events[$k]["gift_quant"]=$GFT->gift_quant;
				$events[$k]["gift_type"]=count($GFT->gifts);
				$events[$k]["gift_ids"]=array_merge($GFT->gift_ids);
				$events[$k]["gift_names"]=$GFT->gift_names;

				$events[$k]["players"] = $PLR->where("`e_player`.`event_id`=".$k)->group("id")->getField("id,epid,player_name,player_descrip,event_player_descrip,is_team,title");
				$events[$k]["player_quant"] = count($events[$k]["players"]);
				// echo $PLR->_sql();
				// echo $PLR->getDbError();
			}
			$this->assign('url',C("URL"));
			$this->assign('q',$q);
			$this->assign('page',$page);
			$this->assign('events',$events);
			$this->assign('curtool',$curtool);
			$this->display();
		}
		public function step1(){
			$create_new = I("create");
			if($create_new){ //带有创建标识的清空活动ID
				session("event_id",null);
				session("title",null);
				session("start",null);
				session("pic_url",null);
				session("descrip",null);
				session("game_id",null);
				session("game_name",null);
				session("player",null);
				session("player_id",null);
				session("player_descrip",null);
				session("gifting_time",null);
				session("cffs",null);
				session("jzsj",null);
				session("jzfs",null);
				session("cyjf",null);
				session("zmc",null);
				session("event_title",null);
				session("multi_yes",null);
				session("multi_no",null);
				session("privat_league_id",null);
				session("league_name",null);
				session("game_category",null);
			}
			$this->assign('cyjf',session("cyjf"));
			$this->assign('jzsj',session("jzsj"));
			$this->assign('jzfs',session("jzfs"));
			$this->assign('cffs',session("cffs"));
			$this->assign('zmc',session("zmc"));
			$this->assign('gifting_time',session("gifting_time"));
			$this->assign('title',session("title"));
			$this->assign('start',session("start"));
			$this->assign('pic_url',session("pic_url"));
			$this->assign('descrip',session("descrip"));
			$this->assign('game_id',session("game_id"));
			$this->assign('game_name',session("game_name"));
			$this->assign('event_title',session("event_title"));
			$this->assign('game_category',session("game_category"));
			$this->display();
		}
		public function step2(){
			$start = I("start");
			$pic_url = I("pic_url");
			$descrip = I("descrip");
			$game_id = I("game_id")?I("game_id"):session("game_id");
			$game_name = I("game_name");
			$game_category = I("game_category");
			$privat_league_id = I("privat_league_id");
			$league_id = I("league_id");
			$league_name = I("league_name");
			$team_type = I("team_type");
			$cffs = I("cffs")=="input"?I("savecffs"):I("cffs");
			$jzsj = I("jzsj")?I("jzsj"):0;
			$jzfs = I("jzfs")?I("jzfs"):0;
			$cyjf = I("cyjf")?I("cyjf"):0;
			$zmc = I("zmc")?I("zmc"):0;
			$gifting_time = I("gifting_time");
			$event_title = I("event_title");
			
			if($game_id){
				 // 保存已设置的比赛内容
				session("privat_league_id",$privat_league_id);
				session("league_name",$league_name);
				session("team_type",$team_type);
				session("start",$start);
				session("pic_url",$pic_url);
				session("descrip",$descrip);
				session("game_id",$game_id);
				session("game_name",$game_name);
				session("game_category",$game_category);
				session("cffs",$cffs);
				session("jzsj",$jzsj);
				session("jzfs",$jzfs);
				session("cyjf",$cyjf);
				session("zmc",$zmc);
				session("gifting_time",$gifting_time);
				session("event_title",$event_title);
				$GAME = M("KxdrGames");
				$data["edit_time"]=date("Y-m-d H:i:s",time());
				$data["start_time"]=$start;
				$data["pic_url"]=$pic_url;
				$data["name"]=$game_name;
				$data["description"]=$descrip;
				$GAME->where("`id`=$game_id AND `sid`=".$this->sid)->save($data);
			}else{
				$this->success("您没有选择活动项目！",U("index/step1?create=1"));
				die();
			}
			
			$event["game_id"] = session("game_id");
			$event["title"] = session("event_title");
			$event["need_seeds"] = session("cyjf");
			$event["think_type"] = session("cffs");
			$event["think_type_way"] = session("zmc");
			$event["time_out"] = session("jzsj");
			$event["time_out_type"] = session("jzfs");
			$event["pic_url"] = session("pic_url");
			$event["gifting_time"] = session("gifting_time");
			$event["description"] = session("descrip");
			$event["multi_yes"] = session("multi_yes")?session("multi_yes"):0;
			$event["multi_no"] = session("multi_no")?session("multi_no"):0;
			$event["edit_time"] = date("Y-m-d H:i:s");
			$event["sid"] = $this->sid;
			$event_id = session("event_id");
			$EVENT = M("KxdrEvents");
			if(!$event_id){
				$event["version"] = session("app_version");
				$event_id = $EVENT->add($event);
				session("event_id",$event_id);
			}else{
				$EVENT->where("`id`=".$event_id)->save($event);
			}
			
			$PLAYER = D("EventPlayerView");
			$players = $PLAYER->where("`e_player`.`event_id`=".session("event_id")." AND `e_player`.`sid`=".$this->sid)->getField("epid,event_player_descrip,title,think_type,think_type_way,think_type_sign,target_data,player_name,player_descrip,is_team");
			foreach($players as $k=>$p){
				foreach(C("KXDR_THINK_WAY") as $way){
					if($p["think_type_way"]==$way["id"]){
						$players[$k]["think_type_way_name"]=$way["name"];break;
					}
				}
			}
			session("players",$players);
			$this->assign('event_id',$event_id);
			$this->assign('team_type',$team_type);
			$this->assign('game_category',session("game_category"));
			$this->assign('privat_league_id',$privat_league_id);
			$this->assign('league_id',$league_id);
			$this->assign('league_name',$league_name);
			$this->assign('players',$players);
			$this->assign('gifts',$gifts);
			$this->assign('cyjf',session("cyjf"));
			$this->assign('jzsj',session("jzsj"));
			$this->assign('jzfs',session("jzfs"));
			$this->assign('cffs',session("cffs"));
			$this->assign('zmc',session("zmc"));
			$this->assign('gifting_time',session("gifting_time"));
			$this->assign('title',session("event_title"));
			$this->assign('start',session("start"));
			$this->assign('pic_url',session("pic_url"));
			$this->assign('descrip',session("descrip"));
			$this->assign('game_id',session("game_id"));
			$this->assign('game_name',session("game_name"));
			$this->assign('event_title',session("event_title"));
			$this->assign('player_title',session("player_title"));
			$this->assign('multi_yes',session("multi_yes"));
			$this->assign('multi_no',session("multi_no"));
			$this->display();
		}
		public function step3(){
			$event_id = session("event_id")?session("event_id"):I("eid");
			$GFT=D("GiftView"); //用视图模型
			$GFT->eid=$event_id;
			$GFT->sid=$this->sid;
			$GFT->getEventGifts();
			$gifts=$GFT->gifts;
			$players = session("players");
			foreach($players as $k=>$player){
				$player_json[]=Array(
					"text"=>$player["player_name"]." 【".$player["think_type"]."】",
					"value"=>$player["epid"],
					"title"=>$player["title"],
					"think_type_way"=>$player["think_type_way"],
					"think_type_sign"=>$player["think_type_sign"],
					"target_data"=>$player["target_data"],
					"think_type_way_name"=>$player["think_type_way_name"]
				);
			}
			$this->assign('player_json',json_encode($player_json)); //奖品列表
			$this->assign('gifts',$gifts); //奖品列表
			$this->assign('cyjf',session("cyjf")); //参与积分
			$this->assign('jzsj',session("jzsj")); //截止时间
			$this->assign('jzfs',session("jzfs")); //截止方式
			$this->assign('cffs',session("cffs")); //猜分方式
			$this->assign('zmc',session("zmc")); //猜分方式，怎么猜？猜是，猜否，猜是、否，猜数据
			$this->assign('players',session("players"));
			$this->assign('gifting_time',session("gifting_time"));
			$this->assign('title',session("event_title"));
			$this->assign('start',session("start"));
			$this->assign('pic_url',strpos(session("pic_url"),"kxdr")?session("pic_url")."@308w_308h.jpg":session("pic_url")."_310x310.jpg");
			$this->assign('descrip',session("descrip"));
			$this->assign('game_id',session("game_id"));
			$this->assign('game_name',session("game_name"));
			$this->display("Index:step3");
		}
		public function step4(){
			$RITEM = M("KxdrRecomItems");
			$items = $RITEM->where("`sid`=".$this->sid." AND `event_id`=".session("event_id"))->getField("num_iid,pic_url,recomment,title");
			$this->assign('items',$items); //参与积分
			$this->assign('gifts',$gifts); //奖品列表
			$this->assign('cyjf',session("cyjf")); //参与积分
			$this->assign('jzsj',session("jzsj")); //截止时间
			$this->assign('jzfs',session("jzfs")); //截止方式
			$this->assign('cffs',session("cffs")); //猜分方式
			$this->assign('zmc',session("zmc")); //猜分方式，怎么猜？猜是，猜否，猜是、否，猜数据
			$this->assign('players',session("players"));
			$this->assign('gifting_time',session("gifting_time"));
			$this->assign('title',session("event_title"));
			$this->assign('start',session("start"));
			$this->assign('pic_url',strpos(session("pic_url"),"kxdr")?session("pic_url")."@308w_308h.jpg":session("pic_url")."_310x310.jpg");
			$this->assign('descrip',session("descrip"));
			$this->assign('game_id',session("game_id"));
			$this->assign('game_name',session("game_name"));
			$this->display();
		}
}